﻿using System;
using System.Collections.Generic;
using System.Linq;
using StyleMVVM.DependencyInjection;
using StyleMVVM.ViewModel;
using System.ComponentModel;
using Wala.Connection.Argument;
using Wala.Connection.Constants;
using Wala.Connection.Objects.Contacts;
using Wala.Infrastructure;
using Wala.Connection.Objects.Timeline;
using Wala.Connection.Managements;
using StyleMVVM.View;
using System.Collections.ObjectModel;
using Windows.UI.Popups;
using Wala.Connection.ComConnections;
using Wala.Connection.Helpers;
using Wala.Connection.Objects.Authen;

namespace Wala.ViewModels.Friends
{
    public class FriendCommonPageViewModel : PageViewModel, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region //------------- Private Fields ------------//

        private DelegateCommand viewProfileCommand;
        private DelegateCommand viewAllTimelineCommand;
        
        private ContactBasic profile;
        private ObservableCollection<TimelineInfo> timelines;
        private RetAuthen retAuthen;

        #endregion

        #region //------------ CLR Properties -------------//

        public ContactBasic Profile
        {
            get
            {
                return profile;
            }
            set
            {
                if (profile != value)
                {
                    profile = value;
                    OnPropertyChanged("Profile");
                }
            }
        }

        public string AvatarLink
        {
            get
            {
                if (profile != null)
                {
                    if (profile.Friend_avatar_id != 0)
                    {
                        return string.Format(URLConstant.GET_AVATAR, profile.Friend_avatar_id);
                    }
                    else
                    {
                        return "ms-appx:///Assets/Login/icon_logo.png";
                    }
                }
                
                return "ms-appx:///Assets/Login/icon_logo.png";
            }
        }     

        public string DisplayName
        { 
            get
            {
                if (profile != null)
                {
                    return profile.Friend_display_name;
                }
                return "";
            }
        }

        public ObservableCollection<TimelineInfo> Timelines
        {
            get { return timelines; }
            set 
            {
                if (timelines != value)
                {
                    timelines = value;
                    OnPropertyChanged("Timelines");
                }
            }
        }

        public DelegateCommand ViewProfileCommand
        {
            get
            {
                if (viewProfileCommand == null)
                {
                    viewProfileCommand = new DelegateCommand(ViewProfileExcute);
                }

                return viewProfileCommand;
            }
        }

        public DelegateCommand ViewAllTimelineCommand
        {
            get
            {
                if (viewAllTimelineCommand == null)
                {
                    viewAllTimelineCommand = new DelegateCommand(ViewTimelineExcute);
                }

                return viewAllTimelineCommand;
            }
        }

        #endregion

        #region //---------- Public Methods ------------//

        public FriendCommonPageViewModel()
        {
            
        }

        [ActivationComplete]
        public async void Active()
        {
            
        }

        #endregion

        #region //--------- Override Methods ------------//

        protected override async void OnNavigatedTo(object sender, StyleNavigationEventArgs e)
        {
            Profile = NavigationParameter as ContactBasic;
            List<TimelineInfo> lstTimeline = new List<TimelineInfo>();
            lstTimeline = await TimelineManager.GetFriendTimeline(Profile.Friend_id, 0, 0);
            Timelines = new ObservableCollection<TimelineInfo>();
            foreach (TimelineInfo timeline in lstTimeline)
            {
                Timelines.Add(timeline);
            }
        }

        #endregion

        #region //----------- Protected Methods ----------//

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region //----------- Private Methods ---------//                

        private void ViewProfileExcute(object parameter)
        {
            Navigation.Navigate("FriendDetailPage", profile);
        }

        private void ViewTimelineExcute(object parameter)
        {
            Navigation.Navigate("TimelinePage", profile);
        }
        #endregion
    }
}